package com.cndw.kungfu.business.gm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.cndw.commons.util.BlankUtils;
import com.cndw.kungfu.comm.AccessInspection;
import com.cndw.kungfu.comm.GmCommand;
import com.cndw.kungfu.comm.LocalCommand;
import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsIntKey;
import com.cndw.kungfu.domain.Goods;
import com.cndw.kungfu.domain.GoodsMode;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.JsonProcesser;
import com.cndw.kungfu.ext.UtilsOfBusiness;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.business.IPayManager;
import com.cndw.kungfu.model.business.IPlayerManager;
import com.cndw.kungfu.model.goods.PlayerBoxs;
import com.cndw.kungfu.vo.PlayerDataVO;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.annotation.UnAuth;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

@Controller
public class BusinessPlayer {

	@Autowired
	private IPlayerManager playerManager;

	@Autowired
	private IPayManager iPayManager;

	/**
	 * 获取列表
	 * 
	 * @param session
	 * @return
	 */
	@UnAuth
	@GmCommand
	public ResponseMessage getPlayers(Session session, String sign, Map<String, Object> paramMap) {

		long playerId = Long.parseLong(paramMap.get("playerId").toString());
		String playerName = BlankUtils.isBlank(paramMap.get("playerName")) ? null : paramMap.get("playerName").toString();
		String accountName = BlankUtils.isBlank(paramMap.get("accountName")) ? null : paramMap.get("accountName").toString();
		int regBeginTime = Integer.parseInt(paramMap.get("regBeginTime").toString());
		int regEndTime = Integer.parseInt(paramMap.get("regEndTime").toString());
		int loginBeginTime = Integer.parseInt(paramMap.get("loginBeginTime").toString());
		int loginEndTime = Integer.parseInt(paramMap.get("loginEndTime").toString());
		int pageSize = Integer.parseInt(paramMap.get("pageSize").toString());
		int pageCount = Integer.parseInt(paramMap.get("pageCount").toString());
		pageCount = pageCount == 0 ? 1 : pageCount;

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("players", playerManager.getPlayers(playerId, playerName, accountName, regBeginTime, regEndTime, loginBeginTime, loginEndTime,
				(pageCount - 1) * pageSize, pageSize));
		resultMap.put("totals", playerManager.getPlayersCount(playerId, playerName, accountName, regBeginTime, regEndTime, loginBeginTime, loginEndTime));
		return ResponseMessage.getSuccMessage(resultMap);

	}

	@UnAuth
	@GmCommand
	public ResponseMessage search(Session session, String sign, Map<String, Object> searchMap) {

		int playerType = Integer.parseInt(searchMap.get("playerType").toString());
		String player = BlankUtils.isBlank(searchMap.get("player")) ? null : searchMap.get("player").toString();
		long playerId = UtilsOfBusiness.getPlayerId(playerType, player);
		String transactionId = BlankUtils.isBlank(searchMap.get("transactionId")) ? null : searchMap.get("transactionId").toString();
		int exchangeType = Integer.parseInt(searchMap.get("exchangeType").toString());
		int beginTime = Integer.parseInt(searchMap.get("beginTime").toString());
		int endTime = Integer.parseInt(searchMap.get("endTime").toString());
		int pageSize = Integer.parseInt(searchMap.get("pageSize").toString());
		int pageCount = Integer.parseInt(searchMap.get("pageCount").toString());
		pageCount = pageCount == 0 ? 1 : pageCount;

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("recharges", iPayManager.getRecharge(playerId, transactionId, exchangeType, beginTime, endTime, (pageCount - 1) * pageSize, pageSize));
		resultMap.put("totals", iPayManager.getRechargeCounts(playerId, transactionId, exchangeType, beginTime, endTime));
		return ResponseMessage.getSuccMessage(resultMap);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage getInfo(Session session, String sign, int type, String param) {
		return ResponseMessage.getSuccMessage(UtilsOfBusiness.getPlayer(type, param));
	}

	@UnAuth
	@LocalCommand
	public ResponseMessage getPlayerData(Session session, String sign, int type, String param) {
		Player player = UtilsOfBusiness.getPlayer(type, param);
		if (player == null) {
			return ResponseMessage.getFailMessage(false);
		}
		PlayerDataVO dataVO = new PlayerDataVO();
		dataVO.setPlayerId(player.getPlayerId());
		dataVO.setAccountName(player.getAccountName());
		dataVO.setPlayerName(player.getPlayerName());
		dataVO.setPrestige2(player.getDataInfo().getPrestige(2));
		dataVO.setPrestige3(player.getDataInfo().getPrestige(3));
		dataVO.setPrestige4(player.getDataInfo().getPrestige(4));
		dataVO.setQuizIntegral(player.getDataInfo().intDataGet(ConstantsIntKey.quizScore));
		return ResponseMessage.getSuccMessage(dataVO);
	}

	@UnAuth
	@LocalCommand
	public ResponseMessage clearPlayerBox(Session session, String sign, int type, String param) {
		Player player = UtilsOfBusiness.getPlayer(type, param);
		if (player == null) {
			return ResponseMessage.getFailMessage(false);
		}		
		for (Goods g : player.getPlayerBoxs().getBoxsGoods()) {
			player.getPlayerBoxs().remove(g.getStoragePlace());
		}
		return ResponseMessage.getSuccMessage(true);
	}

	@UnAuth
	@LocalCommand
	public ResponseMessage updatePlayerData(Session session, String sign, Map<String, Object> sendMap) {

		if (sendMap.containsKey("playerId")) {
			long playerId = Long.valueOf(sendMap.get("playerId").toString());
			Player player = Context.getPlayerServer().getByPlayerId(playerId);

			if (sendMap.containsKey("prestige2")) {
				int alter = Integer.parseInt((String) sendMap.get("prestige2")) - player.getDataInfo().getPrestige(2);
				player.getDataInfo().alterPrestige(2, alter);
				Context.getGeneralServer().addTilte(player.getPlayerId(), Constants.titlePrestige, 0);
			}

			if (sendMap.containsKey("prestige3")) {
				int alter = Integer.parseInt((String) sendMap.get("prestige3")) - player.getDataInfo().getPrestige(3);
				player.getDataInfo().alterPrestige(3, alter);
			}

			if (sendMap.containsKey("prestige4")) {
				int alter = Integer.parseInt((String) sendMap.get("prestige4")) - player.getDataInfo().getPrestige(4);
				player.getDataInfo().alterPrestige(4, alter);
			}

			if (sendMap.containsKey("quizIntegral")) {
				player.getDataInfo().intDataAlter(ConstantsIntKey.quizScore,
						Integer.parseInt((String) sendMap.get("quizIntegral")) - player.getDataInfo().intDataGet(ConstantsIntKey.quizScore));
			}

		}

		return ResponseMessage.getSuccMessage("ok");
	}

	@UnAuth
	@LocalCommand
	public ResponseMessage updatePlayer(Session session, String sign, Map<String, Object> sendMap) {

		if (sendMap.containsKey("playerId")) {
			long playerId = Long.valueOf(sendMap.get("playerId").toString());
			Player player = Context.getPlayerServer().getByPlayerId(playerId);
			if (player == null) {
				return ResponseMessage.getFailMessage(false);
			}
			UtilsOfBusiness.execUpdate(player, sendMap);
			Context.getSyncServer().syncDB(player);
		}

		return ResponseMessage.getSuccMessage("ok");
	}

	@UnAuth
	@GmCommand
	public ResponseMessage getBox(Session session, String sign, int style, String param) {
		Player player = UtilsOfBusiness.getPlayer(style, param);
		if (!BlankUtils.isBlank(player)) {
			PlayerBoxs playerBoxs = player.getPlayerBoxs();

			Map<Integer, List<String[]>> map = new HashMap<Integer, List<String[]>>();
			final Integer equipType = new Integer(1); // 装备
			final Integer gemType = new Integer(2); // 宝石
			final Integer bookType = new Integer(3); // 技能书
			final Integer drugType = new Integer(4); // 药品
			final Integer groceriesType = new Integer(5); // 杂货
			for (Goods goods : playerBoxs.getAllGoods()) {
				GoodsMode mode = goods.getGoodsMode();
				Integer type = null;
				if (mode.getRootId() == 2) {
					type = equipType;
				} else if (mode.getGoodTypeId() == 168 || mode.getGoodTypeId() == 104 || mode.getGoodTypeId() == 105) {
					type = gemType;
				} else if (mode.getGoodTypeId() == 102 || mode.getGoodTypeId() == 106) {
					type = bookType;
				} else if (mode.getGoodTypeId() == 101) {
					type = drugType;
				} else if (mode.getGoodTypeId() == 188 || mode.getGoodTypeId() == 103 || mode.getGoodTypeId() == 107) {
					type = groceriesType;
				} else {
					continue;
				}

				if (!map.containsKey(type)) {
					map.put(type, new ArrayList<String[]>());
				}

				map.get(type).add(
						new String[] { String.valueOf(goods.getStorageType()), String.valueOf(goods.getStoragePlace()), String.valueOf(goods.getStackNum()),
								String.valueOf(mode.getGoodsId()), mode.getName() });
			}
			return ResponseMessage.getSuccMessage(map);
		}

		return ResponseMessage.getSuccMessage(null);
	}

	@UnAuth
	@GmCommand
	@SuppressWarnings("rawtypes")
	public ResponseMessage delGoods(Session session, String sign, int style, String param, String goodsParam) {
		Player player = UtilsOfBusiness.getPlayer(style, param);
		if (!BlankUtils.isBlank(player)) {
			try {
				PlayerBoxs playerBoxs = player.getPlayerBoxs();
				List<List> goodsList = JsonProcesser.objectJsonArrayDecode(goodsParam, List.class);
				for (List<String> g : goodsList) {
					int type = Integer.parseInt(g.get(0));
					int stroage = Integer.parseInt(g.get(1));
					int counts = Integer.parseInt(g.get(2));
					int goodsId = Integer.parseInt(g.get(3));

					if (counts < 0) {
						if (type == PlayerBoxs.typeBox) {
							Goods goods = playerBoxs.get(stroage);
							if (!BlankUtils.isBlank(goods) && goods.getGoodsId() == goodsId) {
								int num = goods.getStackNum() + counts;
								if (num <= 0) {
									playerBoxs.remove(stroage);
								} else {
									goods.alertStackNum(counts);
								}
								goods.commitSync();
							}
						} else if (type == PlayerBoxs.typeHold) {
							Iterator<Goods> iterator = playerBoxs.getHoldGoods().iterator();
							while (iterator.hasNext()) {
								Goods goods = iterator.next();
								if (goods.getStoragePlace() == stroage && goods.getGoodsId() == goodsId) {
									int num = goods.getStackNum() + counts;
									if (num <= 0) {
										iterator.remove();
										goods.setStackNum(0);
									} else {
										goods.alertStackNum(counts);
									}
									goods.commitSync();
									break;
								}
							}
						}
					}
				}
				return ResponseMessage.getSuccMessage(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return ResponseMessage.getSuccMessage(false);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage deduction(Session session, String sign, int style, String param, String assets, String golds, String goldTickets) {
		int asset = Integer.parseInt(assets);
		int gold = Integer.parseInt(golds);
		int goldTicket = Integer.parseInt(goldTickets);
		Player player = UtilsOfBusiness.getPlayer(style, param);
		if (!BlankUtils.isBlank(player)) {

			boolean change = false;
			if (gold < 0) {
				change = true;
				player.alterGold(gold);
				if (player.getGoldAll() < 0) {
					
				}
			}

			if (asset < 0) {
				change = true;
				player.alterAsset(asset);
				if (player.getAsset() < 0) {
					//player.setAsset(0);
				}
			}

			if (goldTicket < 0) {
				change = true;
				player.alterGoldTicke(goldTicket);
				if (player.getGoldTicke() < 0) {
					player.setGoldTicke(0);
				}
			}

			if (change) {
				player.commitSync();
			}

			return ResponseMessage.getSuccMessage(true);
		}
		return ResponseMessage.getSuccMessage(false);
	}
	@UnAuth
	@GmCommand
	public ResponseMessage gmNameChange(Session session, String sign,String acc,String name){
		if (!AccessInspection.isGmInstructor(acc)) {
			return ResponseMessage.getSuccMessage(false);
		}
		Player player = Context.getPlayerServer().getByPlayerName(acc);
		if (player == null) {
			return ResponseMessage.getSuccMessage(false);
		}
		player.setPlayerName(name);
		player.setVocationId(3);
		player.setSex(2);
		player.commitSync();
		return ResponseMessage.getSuccMessage(true);
	}
}
